import fs, { outputFileSync } from "fs-extra";
import postcss from "rollup-plugin-postcss";
import analyze from "rollup-plugin-analyzer";
import multiEntry from "rollup-plugin-multi-input";

// ! 不要妄想使用 importsmap 导入 @codingame/monaco-editor 或者是其他库，这些库没有后缀名，属于 nodejs 代码文件

fs.emptyDir("./dist/editor/");
// rollup.config.js
export default {
    external: [],
    input: ["./node_modules/@codingame/monaco-editor/esm/**/*.js"],
    output: {
        dir: "./dist/editor",
        format: "es",
    },
    plugins: [
        multiEntry({
            transformOutputPath: (output, input) => {
                return output.replace(
                    "node_modules\\@codingame\\monaco-editor\\esm\\vs\\",
                    ""
                );
            },
        }),

        {
            resolveId(thisFile, importer) {
                if (thisFile.startsWith("vscode")) {
                    return {
                        external: true,
                        id: "../api/" + thisFile.replace(/.*\//, ""),
                    };
                }
                if (thisFile.startsWith("https://")) {
                    return false;
                }

                if (!/^[[\w:]|\.|\/]/.test(thisFile)) console.log(thisFile);
            },
        },

        // nodeResolve({
        //     browser: true,
        //     extensions: [".ts", ".tsx", ".js"],
        // }),
        // babel({
        //     babelHelpers: "bundled",
        //     extensions: [".ts", ".tsx", ".js", ".svg"],
        //     exclude: ["**/@codingame/**/*"],
        // }),
        postcss({
            inject: true,
            minimize: {},
            modules: {},
            sourceMap: false,
            extensions: [".css", ".less"],
        }),
        analyze({
            summaryOnly: true,
            writeTo: (str) =>
                outputFileSync("dist/analyze/editor.analyze.txt", str),
        }),
    ],
};
